package com.sishu.njrtsdms.service.impl;


import com.sishu.njrtsdms.domain.core.GenericServiceImpl;
import com.sishu.njrtsdms.domain.core.PageInfo;
import com.sishu.njrtsdms.domain.dto.UserDTO;
import com.sishu.njrtsdms.domain.vo.UserVO;
import com.sishu.njrtsdms.exception.ServiceException;
import com.sishu.njrtsdms.jooq.codegen.tables.daos.CollegeDAO;
import com.sishu.njrtsdms.jooq.codegen.tables.daos.UserDAO;
import com.sishu.njrtsdms.jooq.codegen.tables.pojos.User;
import com.sishu.njrtsdms.service.UserService;
import com.sishu.njrtsdms.utils.MyBeanUtils;
import com.sishu.njrtsdms.utils.SecurityUtils;

import org.jooq.types.ULong;
import org.springframework.stereotype.Service;


@Service
public class UserServiceImpl extends GenericServiceImpl<User, ULong>
        implements UserService {


    private final UserDAO userDAO;

    private final CollegeDAO collegeDAO;


    public UserServiceImpl(UserDAO userDAO,
                           CollegeDAO collegeDAO) {
        super(userDAO);
        this.userDAO = userDAO;
        this.collegeDAO = collegeDAO;
    }


    @Override
    public void updateUserLoginInfo(User user) {
        update(user);
    }

    @Override
    public UserVO selectUserByUsername(String username) {
        return userDAO.selectUserByUsername(username);
    }

    @Override
    public PageInfo<UserVO> page(UserDTO userDTO) {
        return userDAO.page(userDTO);
    }

    @Override
    public UserVO queryOneUserByUserId(ULong userId) {
        return userDAO.fetchOneUserVOByUserId(userId);
    }

    @Override
    public void addUser(UserDTO userDTO) {
        var user = MyBeanUtils.copyBean(userDTO, User.class);
        ULong collegeId = userDTO.collegeId();
        if (collegeId != null && !collegeDAO.existsById(collegeId))
            throw new ServiceException("学院不存在");
        user.setSexDict(userDTO.sexDictId());
        user.setUserTypeDict(userDTO.userTypeDictId());
        user.setUserStatusDict(userDTO.userStatusDictId());
        user.setPassword(SecurityUtils
                .generateDBStorePassword(user.getPassword(), true));
        userDAO.insertSelective(user);
    }

    @Override
    public void updateUser(ULong userId, UserDTO userDTO) {
        if (!userDAO.existsById(userId))
            throw new ServiceException("用户不存在");
        var user = MyBeanUtils.copyBean(userDTO, User.class);
        var collegeId = user.getCollegeId();
        if (collegeId != null && !collegeDAO.existsById(collegeId))
            throw new ServiceException("学院不存在");
        user.setUserId(userId);
        user.setSexDict(userDTO.sexDictId());
        user.setUserTypeDict(userDTO.userTypeDictId());
        user.setUserStatusDict(userDTO.userStatusDictId());
        user.setPassword(SecurityUtils
                .generateDBStorePassword(user.getPassword(), false));
        userDAO.updateSelective(user);
    }

    @Override
    public Long totalCount(UserDTO userDTO) {
        return userDAO.totalCount(userDTO);
    }

    @Override
    public void deleteUser(ULong userId) {
        var dbUser = userDAO.fetchOneByUserId(userId);
        if (dbUser == null || dbUser.getIsDeleted())
            throw new ServiceException("用户不存在或用户已被删除");

        User user = new User();
        user.setUserId(userId);
        user.setIsDeleted(Boolean.TRUE);

        userDAO.updateSelective(user);

        // todo 需要关联处理的也有一些...
    }


}